<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.code }}
        </a>
      </template>
      <template #toolbar>
        <a-button type="primary" ghost @click="handleExportExcel()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <RepairForm @register="registerRepairForm" @success="handleSuccess" />
    <KeepForm @register="registerKeepForm" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <AddQtyModal @register="registerUseQtyModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasMoldBasMoldFindKeepList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { basMoldDelete, basMoldListData, findKeepListData } from '/@/api/bas/mold/basMold';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import RepairForm from '/@/views/mold/repair/asdMoldUpdate/form.vue';
  import KeepForm from '/@/views/mold/keep/asdMoldKeep/form.vue';
  import FormImport from './formImport.vue';
  import AddQtyModal from './addQtyModal.vue';
  import { downloadByUrl } from '/@/utils/file/download';
  import { basMoldClsTreeData } from '/@/api/bas/mold/basMoldCls';

  const props = defineProps({
    treeCode: String,
    treeName: String,
  });

  const { t } = useI18n('bas.mold.basMold');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('保养预警管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('工装代码'),
        field: 'caddcode',
        component: 'Input',
      },
      {
        label: t('工装编码'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('工装名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('工装类型'),
        field: 'clsCode',
        fieldLabel: 'basMold.basMoldCls.treeName',
        component: 'TreeSelect',
        componentProps: {
          api: basMoldClsTreeData,
          allowClear: true,
          onChange: handleSuccess,
        },
      },

      {
        label: t('存货代码'),
        field: 'basInv.cinvaddcode',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'basInv.cinvcode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'basInv.cinvname',
        component: 'Input',
      },
    ],
    fieldMapToTime: [
      ['buyDate', ['buyDate_gte', 'buyDate_lte']],
      ['makeDate', ['makeDate_gte', 'makeDate_lte']],
      ['installDate', ['installDate_gte', 'installDate_lte']],
      ['checkDate', ['checkDate_gte', 'checkDate_lte']],
      ['useDate', ['useDate_gte', 'useDate_lte']],
      ['expiryDate', ['expiryDate_gte', 'expiryDate_lte']],
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('工装编码'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 230,
      align: 'left',
      fixed: 'left',
      // slot: 'firstColumn',
    },
    {
      title: t('工装尾号'),
      dataIndex: 'lastNum',
      key: 'a.lastNum',
      sorter: true,
      width: 90,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('工装名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 230,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('工装代码'),
      dataIndex: 'caddcode',
      key: 'a.caddcode',
      sorter: true,
      width: 130,
      align: 'left',
      //fixed: 'left',
    },
    {
      title: t('工装状态'),
      dataIndex: 'moldStatus',
      key: 'a.mold_status',
      sorter: true,
      dictType: 'bas_mold_status',
      width: 100,
      align: 'center',
      //fixed: 'left',
    },
    {
      title: t('工装类型'),
      dataIndex: 'basMoldCls.name',
      key: 'cls.code',
      sorter: true,
      width: 100,
      align: 'center',
      //fixed: 'left',
    },
    {
      title: t('存货代码'),
      dataIndex: 'basInv.cinvaddcode',
      key: 'inv.cinvcode',
      // sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('存货编码'),
      dataIndex: 'basInv.cinvcode',
      key: 'inv.cinvcode',
      // sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      // sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('累计加工数'),
      dataIndex: 'useQty',
      key: 'a.use_qty',
      sorter: true,
      width: 125,
      align: 'right',
      //fixed: 'right',
    },
    {
      title: t('追加次数'),
      dataIndex: 'addQty',
      key: 'a.add_qty',
      sorter: true,
      width: 100,
      align: 'right',
      //fixed: 'right',
    },
    {
      title: t('保养周期'),
      dataIndex: 'byQty',
      key: 'a.by_qty',
      sorter: true,
      width: 100,
      align: 'right',
      //fixed: 'right',
    },
    {
      title: t('预警比例(%)'),
      dataIndex: 'proportion',
      key: 'a.proportion',
      sorter: true,
      width: 100,
      align: 'right',
      //fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        label: t('维修'),
        onClick: handleRepairForm.bind(this, {
          name: record.name,
          'basMold.caddcode': record.code,
          'basMoldCls.name': record.basMoldCls.name,
          'basMold.clsCode': record.basMoldCls.code,
          lastNum: record.lastNum,
          moldCode: record.caddcode,
        }),
        auth: 'mold:repair:asdMoldUpdate:edit',
      },
      {
        label: t('保养'),
        onClick: handleKeepForm.bind(this, {
          name: record.name,
          'basMold.caddcode': record.code,
          'basMoldCls.name': record.basMoldCls.name,
          'basMold.clsCode': record.basMoldCls.code,
          lastNum: record.lastNum,
          moldCode: record.code,
          useQty: record.useQty,
          useBlQty: record.useBlQty,
          byQty: record.byQty,
        }),
        auth: 'mold:keep:asdMoldKeep:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerRepairForm, { openDrawer: repairOpenDrawer }] = useDrawer();
  const [registerKeepForm, { openDrawer: keepOpenDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: findKeepListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    // rowSelection: {
    //   type: 'checkbox',
    // },
  });

  watch(
    () => props.treeCode,
    async () => {
      await getForm().setFieldsValue({
        clsCode: props.treeCode,
        'basMold.basMoldCls.treeName': props.treeName,
      });
      reload();
    },
  );

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleRepairForm(record: Recordable) {
    repairOpenDrawer(true, record);
  }
  function handleKeepForm(record: Recordable) {
    keepOpenDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/bas/mold/basMold/exportData',
      target: '_self',
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const res = await basMoldDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  const [registerUseQtyModal, { openModal: useQtyModal }] = useModal();
  // 增加使用次数
  async function handleUseQty(record: Recordable) {
    if (getSelectRowKeys().length) {
      let selIds = getSelectRowKeys().join(',');
      useQtyModal(true, { selIds: selIds });
    } else {
      showMessage(t('请先选择一行记录！'));
    }
  }

  function handleSuccess() {
    reload();
  }
  function handleExportExcel() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/bas/mold/basMold/exportKeepData',
      params: {
        ...getForm().getFieldsValue(),
      },
      target: '_self',
    });
  }
</script>
